//
//  TVAmountView.swift
//  iPhone
//
//  Created by Toj on 8/15/22.
//

import UIKit

protocol TVAmountViewDelegate: NSObjectProtocol {
    
    func amountViewView(_ view: TVAmountView, didTapSegment index: Int, title: String)
}

private let itemRowCount = 6
private let itemVmargin: CGFloat = 16
private let itemWidth: CGFloat = 40
private let itemHeight: CGFloat = 24

class TVAmountView: UIView {
    
    var titles: [String] = [] { didSet {
        contentViews = []
        setupContent()
    }}
    
    weak var delegate: TVAmountViewDelegate?
    
    var selectedIndex = 0 {
        willSet {
            if selectedIndex == newValue { return }
            if contentViews.isEmpty || newValue >= contentViews.count { return }
            selectedView = contentViews[newValue]
        }
    }
    
    var normalGradationColors: [CGColor] = []
    var selectedGradationColors: [CGColor] = []
    var selectedView: UIButton? {
        willSet {
            selectedView?.isSelected = false
            selectedView?.gd_add(colors: normalGradationColors)
            
            newValue?.isSelected = true
            newValue?.gd_add(colors: selectedGradationColors)
        }
    }
    
    private var contentViews: [UIButton] = []
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        clipsToBounds = true
        backgroundColor = .white
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        if contentViews.isEmpty { return }
        
        let selfWidth = frame.width
        let itemHmargin = (selfWidth - itemWidth * CGFloat(itemRowCount)) / CGFloat(itemRowCount - 1)
        var x = CGFloat.zero
        var y = CGFloat.zero
        var index = 0
        contentViews.forEach { view in
            x = CGFloat(index % itemRowCount) * (itemWidth + itemHmargin)
            y = CGFloat(index / itemRowCount) * (itemHeight + itemVmargin)
            view.frame = CGRect(x: x, y: y, width: itemWidth, height: itemHeight)
            view.gd_add(colors: normalGradationColors)
            if index == selectedIndex {
                selectedView = view
            }
            index += 1
        }
    }
    
    @objc private func didTapAmountItem(sender: UIButton) {
        let index = sender.tag - 100
        if index == selectedIndex { return }
        selectedIndex = index
        let title = titles[index]
        delegate?.amountViewView(self, didTapSegment: index, title: title)
    }
    
    deinit {
        // print("-->toj: STCommoditySegmentView deinit")
    }
}

extension TVAmountView {
    private func setupContent() {
        var index = 0
        titles.forEach { title in
            let view = UINormalButton()
            addSubview(view)
            view.tag = index + 100
            view.cm_cornerRadius = 12
            view.setTitle(title, for: .normal)
            view.cm_border(color: "#E8CD8C".to_color)
            view.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            view.setTitleColor(UIColor(hex: "#CC9900"), for: .normal)
            view.setTitleColor(UIColor(hex: "#775E33"), for: .selected)
            view.addTarget(self, action: #selector(didTapAmountItem(sender:)), for: .touchUpInside)
            contentViews.append(view)
            index += 1
        }
    }
}
